import { useEffect } from 'react';
import { WindowListener } from '@/type';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ipcRender = (window as any).listenMain;

export default function useIpcListen() {
    return function <T>(
        type: WindowListener,
        cb: (_: unknown, value: T) => unknown
    ) {
        const cleanEffect = () => {
            ipcRender.remove(type, cb);
        };

        useEffect(() => {
            ipcRender.listen(type, cb);
            return cleanEffect;
        }, []);

        return cleanEffect;
    };
}
